<?php
namespace App\Controller;
use Cake\Collection\Collection;

class MallCategoriesController extends AppController{

    public function initialize()
    {
        parent::initialize();
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.11.17
   * ps：获取系统基础商品分类列表
   * */
    public function getBasesCategories()
    {
        $this->loadModel('Categories');
        $data = $this->Categories->find('children', ['for' => 20])->find('threaded')->toArray();
        $this->G->success('Success', $data);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.11.17
   * ps：批量新增分类
   * */
    public function importCategories()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $categories = $this->request->getData("MallCategories");
        $categories = json_decode($categories, true);
        if(empty($categories) || !is_array($categories)){
            $this->G->error("categories_error");
            return null;
        }
        foreach ($categories as $k=>$v){
            $category = $this->MallCategories->newEntity();
            $category->alias = $v['parent'][0]['title'];
            $category->gather = $k+1;
            $category->parent_id = 0;
            $category->enabled = 1;
            $this->MallCategories->save($category);
            $category = $this->MallCategories->newEntity();
            $category->alias = $v['parent'][1]['title'];
            $category->gather = $k+1;
            $category->parent_id = 0;
            $category->enabled = 1;
            $category_save = $this->MallCategories->save($category);
            if(!empty($v['nodes'])){
                foreach ($v['nodes'] as $key=>$value){
                    $category = $this->MallCategories->newEntity();
                    $category->alias = $value['parent']['title'];
                    $category->parent_id = $category_save->id;
                    $category->enabled = 1;
                    $category_save1 = $this->MallCategories->save($category);
                    foreach ($value['nodes'] as $key1=>$value1){
                        $category = $this->MallCategories->newEntity();
                        $category->alias = $value1['title'];
                        $category->parent_id = $category_save1->id;
                        $category->enabled = 1;
                        $this->MallCategories->save($category);
                    }
                }
            }
        }
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.01
   * ps：新增分类
   * */
    public function add()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $parent_id = $this->request->getData("parent_id");
        $alias = $this->request->getData("alias");
        $enabled = $this->request->getData("enabled");
        $gather = $this->request->getData("gather");
        $category_id = $this->request->getData("category_id");
        $parent_id = empty($parent_id) ? 0 : $parent_id;
        $mall_category = $this->MallCategories->newEntity();
        $mall_category->alias = $alias;
        $mall_category->parent_id = $parent_id;
        $mall_category->enabled = $enabled;
        $mall_category->gather = $gather;
        $mall_category->category_id = $category_id;
        $this->MallCategories->save($mall_category);
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.02
   * ps：修改分类
   * */
    public function update()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $id = $this->request->getData("id");
        $parent_id = $this->request->getData("parent_id");
        $alias = $this->request->getData("alias");
        $enabled = $this->request->getData("enabled");
        $gather = $this->request->getData("gather");
        $category_id = $this->request->getData("category_id");
        $parent_id = empty($parent_id) ? 0 : $parent_id;
        $mall_category = $this->MallCategories->newEntity();
        $mall_category->id = $id;
        $mall_category->alias = $alias;
        $mall_category->parent_id = $parent_id;
        $mall_category->enabled = $enabled;
        $mall_category->gather = $gather;
        $mall_category->category_id = $category_id;
        $this->MallCategories->save($mall_category);
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.02
   * ps：删除分类
   * */
    public function delete()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $id = $this->request->getData("id");
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        $this->MallCategories->deleteAll(['id'=>$id]);
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.02
   * ps：获取分类列表
   * */
    public function getList()
    {
        $title = $this->request->getData("title");
        $where = [];
        if(!empty($title)){
            $where['alias like'] = '%'.$title.'%';
        }
        $categories = $this->MallCategories->find()
            ->select(['id','label'=>'alias','parent_id','enabled','gather','category_id'])
            ->where([$where])
            ->order(['lft asc'])
            ->toArray();
        if(!empty($categories)){
            $collection = new Collection($categories);
            $categories = $collection->nest('id', 'parent_id')->toList();
        }
        $this->G->success('Success', $categories);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.02
   * ps：获取指定分类
   * */
    public function getCategory()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $id = $this->request->getData("id");
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        $data = $this->MallCategories->find()
            ->select(['id','alias','parent_id','enabled','gather','category_id'])
            ->order(['lft asc'])
            ->toArray();
        $collection = new Collection($data);
        $data = $collection->nest('id', 'parent_id')->toList();
        $this->G->success('Success', $data);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.02
   * ps：分类移动
   * */
    public function move()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $id = $this->request->getData("id");
        $action = $this->request->getData("action");
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        if(!in_array($action, ['up','down'])){
            $this->G->error("action_error");
            return null;
        }
        if($action == 'up'){
            $this->MallCategories->moveUp($this->MallCategories->get($id));
        }else{
            $this->MallCategories->moveDown($this->MallCategories->get($id));
        }
        $this->G->success('Success');
    }

}
?>